; -*-Scheme-*-

;************************************************************************/
;*                                                                      */
;* Copyright (c) 2003-2009 Vladimir Tsichevski <tsichevski@gmail.com>   */
;*                                                                      */
;* This file is part of bigloo-lib (http://bigloo-lib.sourceforge.net)  */
;*                                                                      */
;* This library is free software; you can redistribute it and/or        */
;* modify it under the terms of the GNU Lesser General Public           */
;* License as published by the Free Software Foundation; either         */
;* version 2 of the License, or (at your option) any later version.     */
;*                                                                      */
;* This library is distributed in the hope that it will be useful,      */
;* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    */
;* Lesser General Public License for more details.                      */
;*                                                                      */
;* You should have received a copy of the GNU Lesser General Public     */
;* License along with this library; if not, write to the Free Software  */
;* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 */
;* USA                                                                  */
;*                                                                      */
;************************************************************************/
(module
 transform
 (library common) ;; for string* support
 (include "common.sch")
 (extern
  (include "libxml/tree.h")
  (include "libxml/parser.h")
  (include "libxslt/xslt.h")
  (include "libxslt/xsltInternals.h")
  (include "libxslt/transform.h")
  (include "libxslt/xsltutils.h")
  (include "libxslt/extensions.h")
;;  (include "libxslt/security.h")

  (include "libexslt/exsltconfig.h")
  )
 (import xmlregexp xtree xpath xslt xmlio encoding)
 )


; xsltApplyOneTemplate:
; @ctxt:  a XSLT process context
; @node:  the node in the source tree.
; @list:  the template replacement nodelist
; @templ: if is this a real template processing, the template processed
; @params:  a set of parameters for the template or NULL

; Process the apply-templates node on the source node, if params are passed
; they are pushed on the variable stack but not popped, it's left to the
; caller to handle them after return (they may be reused).

(define-func xsltApplyOneTemplate void ((xsltTransformContext ctxt)
					(xmlNode node)
					(xmlNode list)
					(xsltTemplate templ)
					(xsltStackElem params)))

; xsltCallTemplate:
; @ctxt:  a XSLT process context
; @node:  the node in the source tree.
; @inst:  the xslt call-template node
; @comp:  precomputed information

; Process the xslt call-template node on the source node

(define-func xsltCallTemplate void ((xsltTransformContext ctxt)
				    (xmlNode node)
				    (xmlNode inst)
				    (xsltStylePreComp comp)))

; xsltRunStylesheetUser:
; @style:  a parsed XSLT stylesheet
; @doc:  a parsed XML document
; @params:  a NULL terminated array of parameters names/values tuples
; @output:  the URL/filename ot the generated resource if available
; @SAX:  a SAX handler for progressive callback output (not implemented yet)
; @IObuf:  an output buffer for progressive output (not implemented yet)
; @profile:  profile FILE * output or NULL
; @userCtxt:  user provided transform context

; Apply the stylesheet to the document and generate the output according
; to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.

; NOTE: This may lead to a non-wellformed output XML wise !
; NOTE: This may also result in multiple files being generated
; NOTE: using IObuf, the result encoding used will be the one used for
;       creating the output buffer, use the following macro to read it
;       from the stylesheet
;       XSLT_GET_IMPORT_PTR(encoding, style, encoding)
; NOTE: using SAX, any encoding specified in the stylesheet will be lost
;       since the interface uses only UTF8

; Returns the number of by written to the main resource or -1 in case of
;         error.

(define-export (xslt-run-stylesheet-user::int
		style::xslt-stylesheet
		doc::xml-doc
		output::string
		ctxt::xslt-transform-context
		#!optional
		params
		profile)
  (let loop ((planar '())
	     (params (or params '())))
    (match-case
     params
     (()
      (let ((style::xslt-stylesheet style)
	    (doc::xml-doc doc)
	    (params::string* (string-list->string* planar))
	    (ctxt::xslt-transform-context ctxt)
	    (profile::file (or profile (pragma::file "NULL")))
	    (output::string output)
	    (profile::file profile))
    (pragma::int
      "xsltRunStylesheetUser($1, $2, (const char **)$3, $4, NULL, NULL, NULL, $5)"
      style
      doc
      params
      output
					;       sax
					;       i-obuf
					;       profile
      ctxt)))
     
     (((?name . ?value) . ?rest)
      (loop (cons* name value planar)rest))

     (?else
      [error "xslt-run-stylesheet-user"
	     "Invalid 'params' argument, must be list of (name . value) pairs"
	     else]))))
